package gitStudent.dry;



import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author: faris
 * @Date: 2021/9/9 15:32
 */
public class StudentsManager<lo> {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 创建集合容器对象
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("heima001","张三",23,"1999-11-11");
        Student stu2 = new Student("heima002","李四",24,"1999-12-12");
        Student stu3 = new Student("heima003","王五",25,"1999-1-1");
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);


        lo:while (true) {
            // 1. 搭建主界面菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            String choice = sc.next();

            switch (choice) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudents(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;

            }

        }
    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid ;
        while (true){
            System.out.println("请输入学号:");
            sid = sc.next();

            int index = getIndex(list, sid);
            if (index==-1){
                System.out.println("该学号不存在");
            }else {
                System.out.println("请输入姓名");
                String name = sc.next();
                System.out.println("请输入年龄");
                int age = sc.nextInt();
                System.out.println("请输入生日");
                String birthday = sc.next();
                list.set(index,new Student(sid,name,age,birthday));
                System.out.println("修改成功");
                break;
            }
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid ;
        while (true){
            System.out.println("请输入学号:");
            sid = sc.next();

            int index = getIndex(list, sid);
            if (index==-1){
                System.out.println("该学号不存在");
            }else {
                list.remove(index);
                System.out.println("该学生信息已删除");
                break;
            }
        }

    }

    private static void queryStudents(ArrayList<Student> list) {
        //健壮
        if (list.size()==0){
            System.out.println("暂无数据,请添加后重试");
        }else {
            System.out.println(list.size());
            System.out.println("学号\t\t\t 姓名\t\t\t年龄\t\t\t生日");
            System.out.println(list);;
        }
    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid ;
        while (true){
            System.out.println("请输入学号:");
            sid = sc.next();

            int index = getIndex(list, sid);
            if (index==-1){
                System.out.println("请输入姓名");
                String name = sc.next();
                System.out.println("请输入年龄");
                int age = sc.nextInt();
                System.out.println("请输入生日");
                String birthday = sc.next();
                list.add(new Student(sid,name,age,birthday));
                System.out.println("添加成功");
                break;
            }else {
                System.out.println("该学号已被使用");
            }
        }

    }

    private static int getIndex(ArrayList<Student> list, String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getSid().equals(sid)){
                index = i;
                break;
            }
        }
        return index;
    }


}
